package com.example.grape_effect.effet.effect

import com.example.grape_effect.gl.common.ShaderParameter


class PointShakeEffect: LYEffect() {


//    lateinit var interpolator:LYInterpolator
    init {
        name = "pointShakeEffect"
        duration = 2000

    }

    override var fragmentShader = """
       precision highp float;
    varying vec2 textureCoordinate;
    uniform sampler2D inputTexture;
    uniform float intensity;

    void main()
    {
        vec2 dir = textureCoordinate - vec2(0.5,0.5);
// 最终偏移的值：方向 * （1-长度），越靠外偏移越小
        vec2 offset = intensity * normalize(dir)* (1.0- length(dir) );
        vec2 uv =  textureCoordinate + offset;
        gl_FragColor = texture2D( inputTexture,uv);
    }
    """.trimIndent()

    override fun addParameters() {
        super.addParameters()
        parameters["intensity"] = ShaderParameter(
            "intensity",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
    }

    override fun setDefaultParameterValues() {
        super.setDefaultParameterValues()
        parameters["intensity"]?.value = 0.1f

    }

    fun setIntensity(intensity: Float) {
        parameters["intensity"]?.value = intensity
    }


    var intensity2 = 0f
    fun updateIntensity(executedTime: Long) {
        intensity2 -= 0.03f
        if (intensity2 <= -0.1) {
            intensity2 = 0f
        }
        setIntensity(intensity2)
    }

    override fun draw() {
        updateIntensity(System.currentTimeMillis() - startTime)
        super.draw()
    }

}